#! /usr/bin/python2.4

# Copyright 2008 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""check_gvn_revprops.py unittest"""


import unittest

import svn.repos

import gvn.hooks.info
import gvn.hooks.pre_commit.check_gvn_revprops

import testing.hooks.common

# We've stored the changes we need in revision 4 of the test repository
TESTREV = 4


class TestCase(testing.hooks.common.HookTestCase):
  # TODO(epg): This is duplicated a few places.
  def _TestProps(self, prop_name, prop_val, author, path=""):
    """Return RunHook result triple from txn based on arguments.

    Args:
      prop_name: which property to create
      prop_val: which property value to set
      author: who owns that transaction
      path: where to graft the property or "REV" for revprop

    Returns:
      0 (pass), 1 (fail), string (fail and display string)

    Make a txn by setting the property to / and check if author can submit it
    """
    # seed argv with expected arguments during a regular call, and overwrite
    # the ones we need to, with our own values lower down
    argv = ["pre-commit", "repo_name_bogus_ignored", "txnname_bogus_ignored"]
    hi = gvn.hooks.info.HookInfo(argv, userdb=self.userdb)

    # plug in the actual repo and txn objects, from which hi can compute fs
    hi._repo = svn.repos.open(self.repo_name, hi.pool)
    hi._txn = testing.hooks.common.BuildPropTxn(hi.fs, TESTREV,
                       prop_name, prop_val, path, author, "boguslog", hi.pool)

    return gvn.hooks.pre_commit.check_gvn_revprops.RunHook(hi, self._logger)


  def testGvnApprove(self):
    """gvn:approve* blocked as part of commit
    """
    self.assertHookResult(self._TestProps("gvn:approve", "",
                          "polly", "REV"),
                          "gvn:approve* not allowed at commit time")
    self.assertHookResult(self._TestProps("gvn:approved", "",
                          "polly", "REV"),
                          "gvn:approve* not allowed at commit time")
    self.assertHookResult(self._TestProps("gvn:approve:polly", "",
                          "polly", "REV"),
                          "gvn:approve* not allowed at commit time")

  def testGvnSubmitted(self):
    """gvn:submitted blocked as part of commit
    """
    self.assertHookResult(self._TestProps("gvn:submitted", "",
                          "polly", "REV"),
                          "gvn:submitted not allowed at commit time")

  def testGvnChangeInvalid(self):
    """Improperly formatted gvn:change blocked
    """
    self.assertHookResult(self._TestProps("gvn:change", "",
                          "polly", "REV"), "Invalid gvn:change value ")
    self.assertHookResult(self._TestProps("gvn:change", "polly/x8c6",
                          "polly", "REV"),
                          "Invalid gvn:change value polly/x8c6")
    self.assertHookResult(self._TestProps("gvn:change", "polly/x8c6@",
                          "polly", "REV"),
                          "Invalid gvn:change value polly/x8c6@")
    self.assertHookResult(self._TestProps("gvn:change", "polly/x8c6@q",
                          "polly", "REV"),
                          "Invalid gvn:change value polly/x8c6@q")
    self.assertHookResult(self._TestProps("gvn:change", "manuel/x8c6@1",
                          "polly", "REV"),
                          "polly not allowed to submit manuel's change"
                          " (manuel/x8c6@1)")
    self.assertHookResult(self._TestProps("gvn:change", "polly/x8c6@-1",
                          "polly", "REV"),
                          "Invalid revision number for gvn:change -1"
                          " (polly/x8c6@-1)")
    self.assertHookResult(self._TestProps("gvn:change", "polly/x8c6@0",
                          "polly", "REV"),
                          "Invalid revision number for gvn:change 0"
                          " (polly/x8c6@0)")
    # Surely r1000 won't exist in our test repo...
    self.assertHookResult(self._TestProps("gvn:change", "polly/x8c6@1000",
                          "polly", "REV"),
                          "Invalid revision number for gvn:change 1000"
                          " (polly/x8c6@1000)")

  def testGvnChangeValid(self):
    """Poperly formatted gvn:change allowed
    """
    self.assertHookResult(self._TestProps("gvn:change", "polly/x8c6@1",
                          "polly", "REV"), 0)
